function inums = get_inst_nums(names, exact)
% function inums = get_inst_nums(names, exact)
%
% This function will return an array of GM patch numbers that correspond to 
% the supplied names.  An optional argument allows for inexact matching and 
% return of the first partial match.
%
% Input:
%   names [cell] - Cell array of instrument names to look for.
%   exact [bool] - Match instrument names exactly (modulo capitalization)?
%                  If false, we return the first partial match.  If true,
%                  only exact matches are returned. [true]
%
% Output:
%   inums [vec]  - The instrument numbers of the instruments/patches that
%                  we are interested in.
%

% Copyright (C) 2010-2030 Graham Grindlay (grindlay@ee.columbia.edu)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

if nargin < 2
    exact = true;
end

if nargin < 1
    inums = 0:127;
    return;
end

% allow just a single string
if ~iscell(names)
    names = {names};
end
    
% get list of all instrument/patch names
inst_names = get_inst_names([]);

inums = nan*ones(1, length(names));

% now find the ones we are interested in
for n = 1:length(names)
    indx = strfind(lower(inst_names), lower(names{n}));
    tmp = find(~cellfun('isempty', indx));
    if ~isempty(tmp)
       if exact
           part_mat = strcmp(inst_names(tmp), names{n});
           if any(part_mat)
               ndx = find(part_mat);
               inums(n) = tmp(ndx(1));
           end
       else
           inums(n) = tmp(1);
       end
    end
end
